package edu.njpi.vueajax.controller;

import com.google.gson.Gson;
import edu.njpi.vueajax.model.User;
import edu.njpi.vueajax.service.ProductService;
import edu.njpi.vueajax.service.UserService;
import edu.njpi.vueajax.utils.Md5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

//@CrossOrigin服务器端解决跨域问题
@CrossOrigin
//@Controller表示本类是一个控制器
@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("/login")
    @ResponseBody
    public String checkLogin(@RequestBody User user){
        //把明文转换为密文
        user.setPassword(Md5Utils.stringToMD5(user.getPassword()));
        User user1 = userService.getUser(user);
        
        Map<String,String> result=new HashMap<>();
        if(user1!=null){
               result.put("result","ok");
               result.put("msg","登录成功!");
        }else{
            result.put("result","false");
            result.put("msg","登录失败!");
        }
        return new Gson().toJson(result);
    }

}
